package com.itheima.controller;


import com.itheima.Utils.JwtUtils;
import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        log.info("员工登录:{},emp");
        Emp res =  empService.login(emp);
//        登陆成功  生成令牌  下发令牌
        if(res != null){
//            信息封裝map集合
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",emp.getId());
            claims.put("name",emp.getName());
            claims.put("username",emp.getUsername());
//            包含了当前登录的员工信息
          String jwt =   JwtUtils.generateJwt(claims);
            return  Result.success(jwt);
        }
//        登陆失败并返回信息
        return Result.error("用户名或密码错误");
    }
}









